package com.qudian.pay.schedule.controller;

import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qudian.pay.core.model.CommonResp;
import com.qudian.pay.schedule.enums.ConsoleEnum;
import com.qudian.pay.schedule.enums.UserEnum;

@Controller
public class IndexController extends BaseController {
	
	@RequestMapping(value = {"/login","/"}, method = RequestMethod.GET)
	public String login() {
		logger.info("跳转首页");
		return "/login";
	}
	
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ResponseBody
	public CommonResp<Object> login(HttpSession session, String userName, String password) {
		UserEnum user = UserEnum.login(userName,password);
		if(user != null){
			session.setAttribute(ConsoleEnum.LOGIN_USER.getCode(), user);
			return success("登录成功");
		}else{
			return fail("登录名或密码错误");
		}
	}
	
	@RequestMapping(value = "/logout")
	public String logout(HttpSession session) {
		session.invalidate();
		return "/login";
	}
	
	@RequestMapping(value = "/home")
	public String home(Model model) {
		return "redirect:/taskInfo";
	}
}
